import { MockMethod } from 'vite-plugin-mock'
import { Random } from 'mockjs'
import { successResult, errorResult } from './utils'

function getUserList() {
  return [
    {
      user_id: 1,
      username: 'admin',
      password: '123456',
      token: 'token1',
      avatar: Random.image('100x100', '#ccc', '#f00', 'a'),
    },
  ];
}

const userServices: MockMethod[] = [
  {
    url: '/user/login',
    timeout: 500,
    method: 'post',
    response: ({ body }: MockRequestParams) => {
      const { username, password } = body
      const checkUser = getUserList().find(
        (user) => user.username === username && user.password === password,
      )
      if (!checkUser) {
        return resultError('帐号不存在或密码错误')
      }
      const { token } = checkUser
      return successResult({
        token
      })
    }
  }
]

export default userServices;